
angular.module('U9ImHd.controllers')

.controller('PortSettingCtrl', ['$scope', '$interval', 'SerialPort', 'imanDialog', 
    function ($scope, $interval, SerialPort, imanDialog) {
        $scope.data = {
            bindDevices: SerialPort.getExternalDevices(true)
        };

        var _activeDevice, _interval, _times;

        $scope.addDevice = function () {
            var externalDevices = SerialPort.getExternalDevices();
            if (externalDevices.length === 0) {
                imanDialog.toastBottom('没有需要设置串口的外接设备');
                return;
            }
            imanDialog.showList({
                title: '请选择外接设备',
                list: externalDevices,
                valueField: 'Device',
                displayField: 'Name',
                value: 8
            }).then(function (select) {
                if (checkAdded(select.Device)) {
                    imanDialog.toastBottom('该外接设备已添加');
                    return;
                }
                $scope.data.bindDevices.push(select);
            });
        };
        $scope.deleteDevice = function (index, device) {
            $scope.data.bindDevices.splice(index, 1);
            if (device.Port) {
                SerialPort.bindSerialPort(device.Device, "");
            }
        };
        $scope.startObserve = function (device) {
            if (_interval) {
                $interval.cancel(_interval);
            }
            if (device.Observe) {
                SerialPort.close();
                device.Observe = false;
                device.Tip = null;
                _activeDevice = null;
                return;
            }
            if (_activeDevice) {
                _activeDevice.Observe = false;
                _activeDevice.Tip = null;
            } else {
                imanDialog.showLoading();
                SerialPort.open([], {}).finally(function () {
                    imanDialog.hideLoading();
                });
            }
            device.Observe = true;
            device.Tip = '检测中(10秒)...';
            _activeDevice = device;
            _times = 10;
            _interval = $interval(function () {
                _times--;
                if (_times <= 0) {
                    SerialPort.close();
                    $interval.cancel(_interval);
                    _activeDevice.Observe = false;
                    _activeDevice.Tip = null;
                    _activeDevice = null;
                } else {
                    _activeDevice.Tip = '检测中(' + _times + '秒)...';
                }
            }, 1000);
        };

        $scope.$on('serialport.DataReceived', function (e, data, port) {
            if (_activeDevice) {
                SerialPort.bindSerialPort(_activeDevice.Device, port).then(function () {
                    _activeDevice.Port = port;
                });
            }
        });

        $scope.$on('$ionicView.afterLeave', function () {
            SerialPort.close();
        });

        function checkAdded (device) {
            var added = false;
            for (var i = $scope.data.bindDevices.length - 1; i >= 0; i--) {
                if ($scope.data.bindDevices[i].Device === device) {
                    added = true;
                    break;
                }
            }
            return added;
        }
    }
]);